package com.zthz.douxing

import org.grails.databinding.BindingFormat

class Plantrip {

    @BindingFormat(code = 'date.formats.date')
    Date startDate

    @BindingFormat(code = 'date.formats.date')
    Date endDate

    String startCity
    String endCity
    String name
    String aliasName
    Double totalDistance
    Double totalTicketprice
    Integer transTimes //换乘次数
    String totalTaketime

    PlanTripSaveType planTripSaveType

    static hasOne = [personOwner: Person]
    static hasMany = [segments: TripSegment]

    static mapping = {
        segments lazy: true
        planTripSaveType column: 'save_type'
    }
    static constraints = {
        aliasName nullable: true
        startDate validator: {val, obj ->

        }
    }


    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Plantrip{");
        sb.append("id=").append(id);
        sb.append(", startDate=").append(startDate);
        sb.append(", endDate=").append(endDate);
        sb.append(", startCity='").append(startCity).append('\'');
        sb.append(", endCity='").append(endCity).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", totalDistance=").append(totalDistance);
        sb.append(", totalTicketprice=").append(totalTicketprice);
        sb.append(", transTimes=").append(transTimes);
        sb.append(", totalTaketime='").append(totalTaketime).append('\'');
        sb.append(", saveType=").append(planTripSaveType.getId());
        sb.append(", version=").append(version);
        sb.append(", segments=").append(segments);
        sb.append(", personOwner=").append(personOwner);
        sb.append('}');
        return sb.toString();
    }
}

/**
 * 表示该行程保存方式。
 */
enum PlanTripSaveType {
    PERSONAL_COLLECTION(2), //收藏
    MY_PLAN(3), //出行计划
    DO_EXPIRED(31), //已执行的过期计划
    UNDO_EXPIRED(30); //未执行的过期计划

    final Integer type
    private PlanTripSaveType(Integer type) {
        this.type = type
    }

    Integer getId() {
        return type
    }

    static PlanTripSaveType findBySaveType(Integer type) {

        values().find { it ->
            it.type == type
        }
    }
}
